package com.example.a313ldy.yuejian_17.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a313ldy.yuejian_17.Bean.StringBean;
import com.example.a313ldy.yuejian_17.Listener.UserListener;
import com.example.a313ldy.yuejian_17.Model.UserModel;
import com.example.a313ldy.yuejian_17.R;



/**
 * Created by LDY on 2017/9/25.
 */

public class LoginActivity extends AppCompatActivity  implements View.OnClickListener,UserListener {

    private Button login;
    private Button regist;
    public EditText tele;
    public EditText pass;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initViews();
        inEvents();
        login.setOnClickListener(this);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    public void initViews() {
        regist = (Button) findViewById(R.id.regist);
        login = (Button) findViewById(R.id.login);
        tele = (EditText) findViewById(R.id.tele);
        pass = (EditText) findViewById(R.id.pass);

    }


    public void inEvents() {
        regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                String a = tele.getText().toString();
                String b = pass.getText().toString();
                UserModel userModel = new UserModel();
                userModel.getLogin(a, b, this);
                break;
        }
    }

    @Override
    public void onResponse(StringBean stringBean) {

        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
//        savaUser(stringBean.getId());//保存用户id
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
//        finish();
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, "" + msg, Toast.LENGTH_SHORT).show();
    }
}
